<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/25
 * Time: 21:23
 */

namespace app\admin\controller;


use app\admin\model\LogisticalRrule;
use app\admin\controller\Validate;

class Logistical extends Base
{

    /**
     * @param $freight_type
     * @return array
     * 获取物流配置费用
     */
    private function isLogistical($freight_type){
        $logisticalModel = new \app\admin\model\Logistical();

        //统一价格保存
        if($freight_type == $logisticalModel::FREIGHT_TYPE_0){
            $unifiedmoney = input("unifiedmoney")*100;
            $data['unifiedmoney'] = $unifiedmoney;
            return $data;
        }
        //1 按件数计算运费 2  按重量（kg计算运费）保存
        if($freight_type == $logisticalModel::FREIGHT_TYPE_1 || $freight_type == $logisticalModel::FREIGHT_TYPE_2){

            $data['first_money'] = input("post.first_money")*100;
            $data['first_num'] = input("post.first_num");
            $data['continued_num'] = input("post.continued_num");
            $data['continued_money'] = input("post.continued_money")*100;

            return $data;
        }

        return array();
    }

    /**
     * @param $data
     * @return \app\admin\model\Logistical|int|string
     * 保存物流费用类型
     */
    private function saveLogistical($data){
       $logisticalModel = new \app\admin\model\Logistical();
       return $logisticalModel->saveLogistical($data);
    }

    /**
     * @param $data
     * @return LogisticalRrule|int|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 保存当前商户物流规则
     */
    private function saveLogisticalRrule($data){
        $logisticalRruleModel = new LogisticalRrule();
        return $logisticalRruleModel->saveRrule($data);
    }

    /**
     * @param $data
     * @return \app\admin\model\Tenantdistributiontype
     *修改当前商户物流类型
     */
    private function updateLogistics($data){
       $tenantTypeModel = new \app\admin\model\Tenantdistributiontype();
         $res = $tenantTypeModel->updateDeliverMethod($data);
       return $res;
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 设置物流费用
     */
    public function settingLogistical(){
        $logisticalModel = new \app\admin\model\Logistical();
        $freight_type = input("post.freight_type");
        if(Validate::checkPackingcharge(input("post.packingCharge"))){
            $packingCharge = input("post.packingCharge")*100;
        }else{
            return show($logisticalModel::STATUS_INVALID,"包装费格式错误！",input("post.packingCharge"));
        }


        $tenantId = session("TENANT_ID");
        $status = input("post.status");
        $logisticalId = input("post.logisticalId");
        $logistics_type_id = input("post.logistics_type_id");



        if(!$logisticalId){
            $logisticalId = get_uniqid_id();
        }

        $createTime = date("Y-m-d h:i:s",time());


       if(!$logisticalModel->verificationFreightType($freight_type)){
           return show($logisticalModel::STATUS_INVALID,"物流费用类型错误！",$freight_type);
       }


        $param["freight_type"] = $freight_type;
        $param["packingCharge"] = $packingCharge;
        $param["tenantId"] = $tenantId;
        $param["status"] = $status;
        $param["freight_type"] = $freight_type;
        $param["id"] = $logisticalId;
        $param["createTime"] = $createTime;
        $param["tenantDistributionId"] =$logistics_type_id;

        $saveLogisticalResult = self::saveLogistical($param);
        if($saveLogisticalResult){
            //修改物流表配送方式
            $typeParam['status'] = $status;

            $typeParam['tenantDistributionId'] = input("post.logistics_type_id");

            $typeParam['deliverMethod'] = input("post.deliverMethod");


            $updateLogistics = self::updateLogistics($typeParam);

            if(!$updateLogistics){

                return show($logisticalModel::STATUS_INVALID,"修改配送方式失败！",$updateLogistics);

            }

            //根据物流费用类型，获取物流规则参数

           $logisticalRruleParam = $this->isLogistical($freight_type);


           $logisticalRruleParam['logistical_id'] = $logisticalId;

           $logisticalRrule = self::saveLogisticalRrule($logisticalRruleParam);

           if($logisticalRrule){

               return show($logisticalModel::STATUS_VALID,"保存成功！",$logisticalRrule);

           }else{

               return show($logisticalModel::STATUS_INVALID,"保存物流费用规则失败！",$logisticalRrule);

           }

        }else{

            return show($logisticalModel::STATUS_INVALID,"保存失败！",$saveLogisticalResult);

        }
    }
}